<template>
	<view>
		<u-navbar title="确认订单"></u-navbar>
		<view class="wrap">
			<view class="address" @click="$.to('/pagesA/pages/my/address?state=1')">
				<view class="left">
					<image src="../../static/image/a1_.png" mode=""></image>
					<view v-if="!isAdd">添加收货地址</view>
					<view class="info" v-else>
						<view class="name">
							{{address.fullname}} <text>{{address.mobile}}</text>
						</view>
						<view class="add">
							{{address.province + ' ' + address.city + ' ' + address.area + ' ' + address.detailed?address.detailed:''}}
						</view>
					</view>
				</view>
				<u-icon name="arrow-right" color="#ccc"></u-icon>
			</view>

			<view class="goodsBox">
				<view class="goods">
					<image :src="info.image" mode=""></image>
					<view class="goodsInfo">
						<view class="tit">{{info.name}}</view>
						<!-- <view class="xinghao">
							型号：128GB;颜色：暗紫色（请勿现金支付）
						</view> -->
						<view class="price" style="margin-top: 20rpx;" v-if="info.type == 1">￥{{info.price}}</view>
						<view class="price" style="margin-top: 20rpx;color: #fc7802;" v-else>{{info.integral}}幸运币</view>
					</view>
				</view>
				<view class="row">
					<view>购买数量</view>
					<view>x{{info.num}}</view>
				</view>
				<view class="row">
					<view>运费</view>
					<view class="hui" v-if="info.freight_value == 0">免运费</view>
					<view class="hui" v-else>￥{{info.freight_value}}</view>
				</view>
				<!-- <view class="row">
					<view>优惠券</view>
					<view class="hui n-flex-row">
						暂无可使用优惠券
						<u-icon name="arrow-right" ></u-icon>
					</view>
				</view> -->
				<view class="row">
					<view>商品总价</view>
					<view class="pri">￥{{info.price * info.num}}</view>
				</view>
				<view class="row">
					<view>幸运币价格</view>
					<view class="pri">￥{{info.integral * info.num}}</view>
				</view>
				<view class="beizhu">
					<view class="">备注信息</view>
					<u-input v-model="remarks" type="textarea" height="50" placeholder="选填备注信息" />
				</view>
			</view>

			<view class="box1" style="margin-top: 20rpx;" v-if="info.type == 1">
				<u-radio-group style="display: block;" width="34rpx" v-model="paymode" active-color="#000">
					<view class="rowitem payrow" @click="selectPay('wechat')">
						<view class="l">
							<image src="../../static/image/weixinf.png" mode=""></image>
							微信
						</view>
						<u-radio name="wechat"></u-radio>
					</view>
					<!-- #ifdef APP-PLUS || H5 -->
					<view class="rowitem payrow" @click="selectPay('alipay')">
						<view class="l">
							<image src="../../static/image/zhifubao.png" mode=""></image>
							支付宝
						</view>
						<u-radio name="alipay"></u-radio>
					</view>
					<!-- #endif -->
					<view class="rowitem payrow" @click="selectPay('integral')">
						<view class="l">
							<image src="../../static/image/xr.png" mode=""></image>
							幸运币
						</view>
						<u-radio name="integral"></u-radio>
					</view>
				</u-radio-group>
			</view>
		</view>
		<view style="height: 200rpx;">
			
		</view>
		<view class="btm">
			<view class="btmbox">
				<view class="n-flex-row n-align-center">
					<view class="">
						<view v-if="info.type == 1">共{{info.num}}件 | 合计：￥<text class="text">{{price_zj}}</text></view>
						<view v-else>共{{info.num}}件 | 合计：<text class="text" style="color: #fc7802;">{{info.price * info.num}}</text> <text style="color: #fc7802;">幸运币</text></view>
						<view class="yun" v-if="info.freight_value != 0">(含运费￥{{info.freight_value}})</view>
					</view>
					<button @click="submit">提交订单</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				remarks: '',
				paymode: '',
				info: '',
				address: '',
				isAdd: false, //是否有地址
				// #ifdef MP-WEIXIN || H5
				paymode:'wechat',
				// #endif
				
				// #ifdef APP-PLUS
				paymode:'alipay',
				// #endif
				price_zj:0,
			}
		},
		onLoad() {
			this.info = uni.getStorageSync('goods')
			this.getAddress()
			uni.$on('changeAddress', (addId) => {
				console.log(addId, 'id');
				this.upDataAddress(addId);
			})
			this.price_zj = ((this.info.price * this.info.num) + Number(this.info.freight_value));
		},
		onUnload() {
			uni.removeStorageSync('goods')
			uni.$off('changeAddress');
		},
		methods: {
			// 钱购买
			submit() {
				if (!this.address) {
					this.$tip.tip("请选择地址")
					return
				}
				if (this.paymode == "") {
					this.$tip.tip("请选择支付方式")
					return
				}
				this.$http({
					url: "api/pay/go_goods_pay",
					data: {
						goods_id: this.info.id,
						num: this.info.num,
						paymode: this.paymode,
						desc: this.remarks,
						address_id: this.address.id,
						// #ifdef H5
						client: 0,
						// #endif
						// #ifdef MP-WEIXIN
						client: 1,
						// #endif
						// #ifdef APP-PLUS
						client: 2,
						// #endif
					}
				}).then(res => {
					console.log(res);
					if (res.data.code == 1) {
						// #ifdef H5
						this.fun.go_h5pay(res.data.data);
						// #endif
						
						// #ifdef MP-WEIXIN
						this.fun.go_mpwxpay(res.data.data).then(payres => {
							this.$tip.tip("支付成功", "success")
							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/mall/paySuccexx'
								})
							}, 700)
						}).catch(payerr => {
							this.$tip.tip('支付失败')
						});
						// #endif
						
						// #ifdef APP-PLUS
						if(this.paymode=='wechat'){
							this.fun.go_appwxpay(res.data.data.pay_data).then(payres => {
								this.$tip.tip("支付成功", "success") 
								setTimeout(() => {
									uni.redirectTo({
										url: '/pages/mall/paySuccexx'
									})
								}, 700)
							}).catch(payerr => {
								this.$tip.tip('支付失败')
							});
						}else{
							this.fun.go_appalipay(res.data.data.pay_data).then(payres => {
								this.$tip.tip("支付成功", "success") 
								setTimeout(() => {
									uni.redirectTo({
										url: '/pages/mall/paySuccexx'
									})
								}, 700)
							}).catch(payerr => {
								this.$tip.tip('支付失败')
							});
						}
						// #endif
					}else if(res.data.code == 8){ //0元免支付的
						uni.redirectTo({
							url: '/pages/mall/paySuccexx'
						})
					} else {
						this.$tip.tip(res.data.msg)
					}
				
				}).catch(err => {});
				return;
				
			},
			// 更新收货地址
			upDataAddress(id) {
				this.$http({
					url: "api/member/get_address",
					data: {
						address_id: id
					}
				}).then(res => {
					console.log(res, "dizhi");
					if (res.data.code == 1) {
						this.address = res.data.data
						this.isAdd = true
					}

				}).catch(err => {});
			},
			getAddress() {
				this.$http({
					url: "api/member/my_address",
				}).then(res => {
					console.log(res);
					if (res.data.code == 1) {
						if (res.data.data.length == 0) {
							this.isAdd = false
						} else {
							this.isAdd = true
						}
						this.address = res.data.data[0]
					} else {
						this.$tip.tip(res.data.msg)
						this.isAdd = false
					}
				}).catch(err => {});
			},
			// 选择支付方式
			selectPay(mode) {
				this.paymode = mode
				if(mode=='integral'){
					this.price_zj = ((this.info.integral * this.info.num) + Number(this.info.freight_value))
				}else{
					this.price_zj = ((this.info.price * this.info.num) + Number(this.info.freight_value))
				}
			},
		}
	}
</script>

<style>
	@import url("../../common/css/confirmOrder.css");
</style>
